package com.chencs.app.lianmeng.service;

import com.alibaba.druid.util.StringUtils;
import com.chencs.app.AppCommon;
import com.chencs.app.lianmeng.mapper.LMMemberMapper;
import com.chencs.vo.biz.lianmeng.*;
import com.chencs.vo.net.lmrequest.*;
import com.chencs.vo.net.lmresponse.LmLoginResponse;
import com.chencs.vo.net.lmresponse.LmPayAccountResponse;
import com.chencs.vo.net.lmresponse.LmRegisterResponse;
import com.chencs.vo.net.lmresponse.LmTeamResponse;
import com.chencs.vo.net.response.ResponseBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import sun.security.provider.MD5;

import java.util.List;
import java.util.UUID;

@Service
public class LMMemberSer {


    @Autowired
    private LMMemberMapper memberMapper;

    /**
     * 发送注册验证码
     * @param request
     * @return
     */
    public ResponseBase sendMsg(LmMessageRequest request){
        ResponseBase response = new ResponseBase();



        return response;
    }



    /**
     * 帐号密码登录
     * @param request
     * @return
     */
    public LmLoginResponse findUserByPwd(LmLoginRequest request){
        LmLoginResponse response = new LmLoginResponse();

        LmMember member = memberMapper.findUserByMobile(request.getMobile());
        if(member!=null){
            if(member.getPwd().equals(request.getPwd())){
                response.setMember(member);
                response.setCode(AppCommon.SUCCESS);
            }else{
                response.setCode(AppCommon.ERROR);
                response.setMsg("密码输入错误!");
            }
        }else{
            response.setCode(AppCommon.NULL);
            response.setMsg("该手机号码未注册!");
        }

        return  response;


    }



    /**
     * 微信登录
     * @param request
     * @return
     */
    public LmLoginResponse findUserByWXId(LmLoginRequest request){
        LmLoginResponse response = new LmLoginResponse();
        LmMember member = memberMapper.findUserByWXId(request.getWx_id());
        if(member!=null){
            response.setMember(member);
            response.setCode(AppCommon.SUCCESS);
        }else{ //注册
            response.setCode(-1100);
            response.setMsg("该微信暂未绑定!");
        }
        return  response;
    }





    /**
     * 创建账户
     * @param request
     * @return
     */
    public LmRegisterResponse createMember(LmRegisterRequest request){
        LmRegisterResponse response = new LmRegisterResponse();
        String uid  = UUID.randomUUID().toString();
          try{
              LmMember lmMember = new LmMember();
              lmMember.setUid(uid);
        //     lmMember.setMobile(request.getMobile());
        //      lmMember.setPwd(request.getPwd());
              lmMember.setOriginalinvitation(request.getInvitation());
              lmMember.setAvatarurl(request.getAvatarurl());
              lmMember.setPlatform(request.getPlatform());
              lmMember.setGetui_id(request.getGetui_id());
              if(!StringUtils.isEmpty(request.getWx_id())){
                  lmMember.setWx_id(request.getWx_id());
              }
              LmAdPosition lmPid  = memberMapper.findPid();
              lmMember.setMm_id(lmPid.getPid());
              memberMapper.userRegister(lmMember);

              //修改PID 失效
              lmPid.setUid(uid);
              lmPid.setFlag("T");
              memberMapper.updatePid(lmPid);
              //创建账户
              LMAccount account = new LMAccount();
              account.setUid(uid);
              account.setBalance(0);
              account.setFreeze(0);
              memberMapper.createAccount(account);
              response.setCode(AppCommon.SUCCESS);
              response.setMsg("注册成功!");
          }catch (Exception e){
              e.printStackTrace();
              response.setCode(AppCommon.ERROR);
              response.setMsg("帐号注册失败!");
          }

        return response;
    }


    /**
     * 创建提现帐号
     * @param request
     * @return
     */
    public ResponseBase createLMAccount(LmccountRequest request){
        ResponseBase response = new ResponseBase();

        LmPay_account pay_account = new LmPay_account();
        pay_account.setAccount(pay_account.getAccount());
        pay_account.setReal_name(request.getName());
        pay_account.setUid(request.getUid());
        pay_account.setFlag("T");

        memberMapper.createPayAccount(pay_account);
        return response;
    }


    /**
     * 获取提现帐号
     * @param request
     * @return
     */
    public LmPayAccountResponse getPayaccount(LmPayAccountRequest request){
        LmPayAccountResponse response = new LmPayAccountResponse();
        LmPay_account pay_account  = null;
      try {
          pay_account =  memberMapper.getAccountByUid(request.getUid());
          if(pay_account!=null){
              response.setLmPay_account(pay_account);
              response.setCode(AppCommon.SUCCESS);
          }else{
              response.setMsg("帐号获取失败");
              response.setCode(AppCommon.NULL);
          }
      }catch (Exception e){
          response.setMsg("帐号获取失败");
          response.setCode(AppCommon.SYS_ERROR);
      }
        return response;
    }


    /**
     * 获取团队1
     * @param request
     * @return
     */
    public LmTeamResponse getTeamLeve1(LmTeamRequest request){
        LmTeamResponse response = new LmTeamResponse();
        LmMember member = memberMapper.findUserByUid(request.getUid());
        List<LmTeam> lmTeamList = memberMapper.getTeamLevel1(member.getInvitation());

        if(CollectionUtils.isEmpty(lmTeamList)){
            response.setCode(AppCommon.NULL);
        }else{
            response.setTeamList(lmTeamList);
            response.setCode(AppCommon.SUCCESS);
        }
        return response;
    }

    /**
     * 获取团队2
     * @param request
     * @return
     */
    public LmTeamResponse getTeamLeve2(LmTeamRequest request){
        LmTeamResponse response = new LmTeamResponse();
        LmMember member = memberMapper.findUserByUid(request.getUid());
        List<LmTeam> lmTeamList = memberMapper.getTeamLevel2(member.getInvitation());
        if(CollectionUtils.isEmpty(lmTeamList)){
            response.setCode(AppCommon.NULL);
        }else{
            response.setTeamList(lmTeamList);
            response.setCode(AppCommon.SUCCESS);
        }
        return response;
    }

}
